
package com.tinem.platform.module.starter.jpa.repository.rbac;

import com.tinem.platform.module.starter.jpa.entity.rbac.RbacAuthoritiesGroup;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @author fengzhihao
 * @version v1
 * @program: platform
 * @description TODO
 * @site https://fzh.cloud/
 * @company
 * @create 2021-6-11 11:32
 */
public interface RbacAuthoritiesGroupRepository extends JpaRepository<RbacAuthoritiesGroup, String> {
    /**
     select *
     from t_authorities
     where EXISTS (
     select 1 from t_group_authorities where c_authorities = t_authorities.c_authorities and exists (
     select 1 from t_group_members where c_group_id = t_group_authorities.c_group_id  and c_user_id = 1
     )
     ) or exists (
     select 1 from t_user_authorities where c_authority = t_authorities.c_authorities and c_user_id = 1
     )
     */
    @Query(nativeQuery = true,value = "select * \n" +
            "from t_authorities\n" +
            "where EXISTS (\n" +
            "\tselect 1 from t_group_authorities where c_authorities = t_authorities.c_authorities and exists (\n" +
            "\t\tselect 1 from t_group_member where c_group_id = t_group_authorities.c_group_id  and c_user_id = :userId\n" +
            "\t)\n" +
            ") or exists (\n" +
            "\tselect 1 from t_user_authorities where c_authority = t_authorities.c_authorities and c_user_id = :userId\n" +
            ")")
    List<RbacAuthoritiesGroup> findByUser(@Param("userId") String userId);

}